#include "avframequeue.h"
#include "qdebug.h"
#include "qglobal.h"

AVFrameQueue::AVFrameQueue()
{

}

AVFrameQueue::~AVFrameQueue()
{

}

void AVFrameQueue::Abort()
{
    Release();
    queue_.Abort();
}

void AVFrameQueue::Release()
{
    forever {
        AVFrame *pkt = nullptr;
        int ret = queue_.Pop(pkt, 1);
        if(ret < 0) {
            qDebug() <<" Release : "<< "";
            break;
        } else {
            av_frame_free(&pkt);
        }
    }
}

int AVFrameQueue::Size()
{
    return queue_.Size();
}

int AVFrameQueue::Push(AVFrame *val)
{
    AVFrame *tmp_frame = av_frame_alloc();
    av_frame_move_ref(tmp_frame, val);

    return queue_.Push(tmp_frame);
}

AVFrame *AVFrameQueue::Pop(const int timeout)
{
    AVFrame *tmp_frame = nullptr;
    int ret = queue_.Pop(tmp_frame, timeout);
    if(ret < 0) {
        qDebug() <<" AVFrameQueue::Pop : "<< "";
    }
    return tmp_frame;
}

AVFrame *AVFrameQueue::Front()
{
    AVFrame *tmp_frame = nullptr;
    int ret = queue_.Front(tmp_frame);
    if(ret < 0) {
        qDebug() <<" Front : "<< "";
    }
    return tmp_frame;
}
